package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.ISysMenuService;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private ISysRoleMenuService iSysRoleMenuService;

    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * id查询
     *
     * @param id
     * @return
     */
    @Override
    public SysRoleDto rowInfo(String id) {
        SysRole byId = this.getById(id);
        SysRoleDto sysRoleDto = new SysRoleDto();//1
        BeanUtils.copyProperties(byId, sysRoleDto);

        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId, id);
        //所有选中的权限
        List<SysRoleMenu> sysRoleMenuList = iSysRoleMenuService.list(lqw);
        List<Integer> list = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : sysRoleMenuList) {
            list.add(Integer.parseInt(sysRoleMenu.getMenuId()));
        }

        LambdaQueryWrapper<SysMenu> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(SysMenu::getPid, 0).in(SysMenu::getId, list);
        //最高层
        List<SysMenu> sysMenuList = iSysMenuService.list(lqw1);
        List<SysMenuDto> sysMenuDtoList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            SysMenuDto sysMenuDto = new SysMenuDto();//2
            BeanUtils.copyProperties(sysMenu, sysMenuDto);
            sysMenuDtoList.add(sysMenuDto);

            LambdaQueryWrapper<SysMenu> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(SysMenu::getPid, sysMenu.getId()).in(SysMenu::getId, list);
            //第二层
            List<SysMenu> sysMenuList1 = iSysMenuService.list(lqw2);
            List<SysMenuDto> sysMenuDtoList1 = new ArrayList<>();

            for (SysMenu menu : sysMenuList1) {
                SysMenuDto sysMenuDto1 = new SysMenuDto();
                BeanUtils.copyProperties(menu, sysMenuDto1);

                LambdaQueryWrapper<SysMenu> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(SysMenu::getPid, menu.getId()).in(SysMenu::getId, list);
                List<SysMenu> list1 = iSysMenuService.list(lqw3);
                List<SysMenuDto> sysMenuDtoList2 = new ArrayList<>();
                for (SysMenu sysMenu1 : list1) {
                    SysMenuDto sysMenuDto2 = new SysMenuDto();
                    BeanUtils.copyProperties(sysMenu1, sysMenuDto2);
                    sysMenuDtoList2.add(sysMenuDto2);
                }
                sysMenuDto1.setChildren(sysMenuDtoList2);
                sysMenuDtoList1.add(sysMenuDto1);
            }
            sysMenuDto.setChildren(sysMenuDtoList1);
            sysMenuDtoList.add(sysMenuDto);
        }
        sysRoleDto.setAuthList(sysMenuDtoList);
        return sysRoleDto;
    }

    /**
     * 添加角色
     *
     * @param hashMap
     * @return
     */
    @Override
    public ResultData addSysRole(HashMap<String, Object> hashMap) {
        String name = (String) hashMap.get("name");
        String description = (String) hashMap.get("description");
        ArrayList<Integer> menuids = (ArrayList<Integer>) hashMap.get("menuids");

        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        sysRole.setDescription(description);
        this.save(sysRole);

        LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRole::getName, name);
        List<SysRole> list = this.list(lqw);
        if (list.size() != 1) {
            return ResultData.error("名称重复!");
        }
        String id = list.get(0).getId();

        for (Integer menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(id);
            sysRoleMenu.setMenuId(String.valueOf(menuid));
            iSysRoleMenuService.save(sysRoleMenu);
        }
        return ResultData.ok("", "");
    }

    @Override
    public void updateSysRole(HashMap<String, Object> hashMap) {
        String roleId = (String) hashMap.get("id");
        String name = (String) hashMap.get("name");
        String description = (String) hashMap.get("description");
        ArrayList<Integer> menuids = (ArrayList<Integer>) hashMap.get("menuids");

        LambdaUpdateWrapper<SysRole> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(SysRole::getId, roleId);
        SysRole sysRole = new SysRole();
        sysRole.setName(name);
        sysRole.setDescription(description);

        this.update(sysRole,lqw);

        LambdaQueryWrapper<SysRoleMenu> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(SysRoleMenu::getRoleId, roleId);
        iSysRoleMenuService.remove(lqw1);

        for (Integer menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(String.valueOf(menuid));
            iSysRoleMenuService.save(sysRoleMenu);
        }

    }
}

